package org.clever.arkdemo.database;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SqlImportFromDir {

    public static void main(String[] args) throws IOException {

        String url = "jdbc:mysql://lucky";
        String username = "root";
        String password = "Hello1234";


        File version = new File("/Users/admin/job/database-resource/versions");
        if (!version.exists()) {
            version.createNewFile();
        }


        File dir = new File("/Users/admin/job/database-resource/wanfoudata");

        File[] databaseFiles = dir.listFiles();


        List<String> list = FileUtils.readLines(version);

        Set<String> databaseSet = new HashSet<>(list);

//        ExecutorService executorService = Executors.newFixedThreadPool(2);

        for (File databaseFile : databaseFiles) {
            if (databaseFile.isDirectory()) {
                String database = databaseFile.getName();
                File[] sqlFiles = databaseFile.listFiles();
                System.out.println("database=" + database);
                for (File sqlFile : sqlFiles) {

                    if (sqlFile.isFile() && sqlFile.getName().endsWith(".sql")) {
                        String key = database + "/" + sqlFile.getName();
                        if (!databaseSet.contains(key)) {

                            System.out.println("sqlFile=" + key + ",size=" + sqlFile.length() / 1024D + "KB");
                            String sqlText = FileUtils.readFileToString(sqlFile,"utf-8");

                            try (MysqlConnection mysqlConnection = new MysqlConnection(url, username, password)) {
                                mysqlConnection.unAutoCommit();
                                SqlImport.importSqlFile(sqlText, database, mysqlConnection.getConnection());
                                mysqlConnection.commit();
                                System.out.println("导入完成： " + key);
                            } catch (SQLException e) {
                                throw new RuntimeException(e);
                            }
                            System.out.println("-----------------------------------------------");


                            try (FileOutputStream fileOutputStream = new FileOutputStream(version, true)) {
                                fileOutputStream.write(key.getBytes());
                                fileOutputStream.write('\n');
                                fileOutputStream.flush();
                            }

                        }




                    }
                }

            }
        }
    }

    private void importSqlFile(String sqlText, String database, Connection connection) throws SQLException {
//        try (Statement statement = connection.createStatement()) {
//            statement.execute("use " + database);
//            statement.execute(sqlText);
//        }
    }
}
